$.Class("Hyip.Grid",
// static properties
{         
    addParams : function(e, params)
    {
        e.sender.transport.options.read.data = params;
    }    
},
// prototype properties
    {
        // constructor function
        init: function(id) {
            this.id = id;
            
            this.dataSource = this.createDataSource();
            this.createGrid();     
            this.initEvents(id);
        },
        createDataSource: function()
        {
            var dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "/hyip/search",
                        dataType: "json",
                        type: "post",
                        data : {
                            
                        }
                    },
                    update: {
                        url: "/hyip/update",
                        dataType: "json",
                        type: "post",
                    },
                    create: {
                        url: "/hyip/create",
                        dataType: "json",
                        type: "post"						
                    },
                    destroy: {
                        url: "/hyip/delete",
                        dataType: "json",
                        type: "post"
                    },
                    parameterMap: function(options, operation) {
                        if (operation == "create" || operation == "update"){
                            var model = options.models[0];
                            if($("#startDate").val() == "") model.startDate = null;
                            else model.startDate = $("#startDate").val();
                            if($("#endDate").val() == "") model.endDate = null;										
                            else model.endDate = $("#endDate").val();
                            if($("#joinedDate").val() == "") model.joinedDate = null;
                            else model.joinedDate = $("#joinedDate").val();

                            delete(model.joinedDateNumber);
                            delete(model.rate);
                            delete(model.lastPaid);
                            delete(model.lastWithdraw);
                            delete(model.duration);
                            delete(model.grid_index);
                        }

                        return options;
                    }
                },
                change: function (e, data) {									
                    if(e.action == "sync")
                        $("#grid").data("kendoGrid").dataSource.read();	
                },
                error: function (xhr, error) {
                        //console.debug(xhr); console.debug(error);
                },				
                batch: true,
                pageSize: 25,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true,
                schema: {
                    data: "results",
                    total: function(data) {
                        return data.total;
                    },
                    model: {
                        id: "id",
                        fields: {
                            id: { editable: false},
                            domain: { validation: { required: true }},
                            ip: { validation: { required: true }},
                            planType: {defaultValue:'Very short'},
                            profitType: {defaultValue:'Low'},
                            status: {defaultValue:'New'},
                            monitor: {defaultValue:''},
                            adv : {defaultValue:''},
                            payment : {defaultValue:''},
                            feature : {defaultValue:''},
                            hostCountry: {defaultValue:''},
                            startDate: {},
                            endDate: {},							
                            joinedDate: {},				
                            plan: {},
                            joinedDateNumber:{},
                            lastPaid: {},
                            lastWithdraw: {},
                            rate: {},
                            budget:{}
                        }
                    }
                },							
                requestStart: function(e) {   
                    var data = {
                        domain : $("#_domain").val(),
                        status : $("#_status").val(),
                        planType : $("#_planType").val(),
                        hostCountry : $("#_country").val(),
                        monitor : $("#_monitor").val(),
                        ip : $("#_ip").val(),
                        isAjax : true
                    };
                    
                    Hyip.Grid.addParams(e, data);
                }
            });

            return dataSource;
        }, 
        createGrid : function()
        {
            $("#" + this.id).kendoGrid({
	        dataSource: this.dataSource,
	        toolbar: ["create","Reset"],
	        //height: 500,
	        filterable: false,	        
	        pageable: true,
	        columns: [{
                field:"id",
                title:"ID",
                width: 50
            },{
                field:"grid_index",
                title:" ",
                width: 40
            },{
                field:"domain",
                title:"Domain",
                width: 180,
                template: '<a href="" onclick="return showWindowPlan(${id});">${domain}</a>'
            },{
            	title:"Joined",
                field:"joinedDateNumber",         
                width: 70,				
                template: '${joinedDateNumber} days'
            },{
            	title:"Last Paid",
                field:"lastPaid",         
                width: 80,
                template: '<a href="" onclick="return Hyip.updateLastPaid(${id});">${lastPaid}</a>'
            },{
            	title:"Last Withdraw",
                field:"lastWithdraw",         
                width: 110,
                template: '<a href="" onclick="return Hyip.updateLastWithdraw(${id});">${lastWithdraw}</a>'
            },{
            	title:"Rate",
                field:"rate",         
                width: 115,
                template: '<div id="rate_${id}" itemID="${id}" value="${rate}" class="rate"></div>'
            },{
            	title:"Budget $",
                field:"budget",         
                width: 90,
                template: '<a href="" onclick="return Hyip.viewBudget(${id});">$${budget}</a>'
            },{
                field:"status",
                title:"Status",
                width: 75
            },{
            	title:"Start date",
                field:"startDate",
                width: 100
            },{
            	title:"Plan time",
                field:"planTime",         
                width: 75
            },{
            	title:"Life time",
                field:"lifeTime",         
                width: 75
            },{
                field:"planType",
                title:"Plan type",
                width: 100
            },{
                field:"monitor",
                title:"Monitor",
                width: 80
            },{
                field:"adv",
                title:"Adv",
                width: 80
            },{
                field:"payment",
                title:"Payment",
                width: 80
            },{
                field:"feature",
                title:"Feature",
                width: 80
            },{
            	title:"Hosted Country",
                field:"hostCountry",
                width: 120  
            },{ 
                command: ["edit","destroy"], title: "", width: 170 
            },{
            	title:"",
                field:"",   
                template: ''
            }],
            editable: {
                mode: "popup",
                template: $("#teamEditorTemplate").html(),
                update: true,
                destroy: true,				
                confirmation: "Are you sure you want to remove this item ?",
                window:{
                        width:730,
                        center: true
                }
            },
            edit: function(e)
            {		
				
            },
            save: function(e) {
                e.model.dirty = true;
            },
            sortable:true,
            selectable: 'row',
            dataBound: function(e) {                
                $(".rate").each(function(){                   
                    var itemID = $(this).attr("itemID"); 
                    var rate = $(this).attr("value");
                    
                    if(rate == null) rate = 0;
                    
                    $('#' + this.id).raty({
                        half  : true,
                        score : rate,
                        click: function(score, evt) {
                            $.ajax({
                                type: 'post',
                                url: '/hyip/updateRate',
                                data: {
                                    id : itemID,
                                    rate : score
                                },
                                dataType: 'json',
                                success: function(obj){

                                },
                            });
                        }
                    });
                });
            }
        });	
        },
        initEvents : function(id)
        {           
            $(".k-grid-Reset", "#" + this.id).click(function(){
                $("#" + id).data("kendoGrid").dataSource.read();	// Reset grid
                return false;
            });	
        },
        refresh : function()
        {
            $("#" + this.id).data("kendoGrid").dataSource.read();	// Reset grid
        }
})

Hyip.updateLastPaid = function(id)
{
    if(confirm("Have they paid ?"))
    {
        $.ajax({
            type: 'post',
            url: '/hyip/updateLastPaid',
            data: {
                id : id
            },
            dataType: 'json',
            success: function(obj){
                $("#grid").data("kendoGrid").dataSource.read();
            },
        });
    }
    
    return false;
}

Hyip.updateLastWithdraw = function(id)
{
    if(confirm("Have you received ?"))
    {
        $.ajax({
            type: 'post',
            url: '/hyip/updateLastWithdraw',
            data: {
                id : id
            },
            dataType: 'json',
            success: function(obj){
                $("#grid").data("kendoGrid").dataSource.read();
            },
        });
    }
    
    return false;
}

Hyip.viewBudget = function(id)
{
	try{
		Budget.hyipId = id;
		$("#tabstrip").data("kendoTabStrip").select(1);
		gridBudget.refresh();
	}
	catch(ex)
	{
		alert("Err: " + ex);
	}
    return false;
}